#include "thread.hpp"

namespace thread
{
	Thread::Thread(const char * name)
		: _start_flag(false),
		_name(name),
		_id(0)
	{
	}

	Thread::~Thread()
	{
		int ret = 0;
		if (_start_flag && (ret = ::pthread_join(_id, 0)) != 0)
		{
			tools::LOG_ERR_MSG(ret, "Thread::~Thread pthread_join");
		}
	}

	int Thread::start()
	{
		if (_start_flag)
		{
			return EINVAL;
		}

		_start_flag = true;

		int ret = 0;
		if ((ret = ::pthread_create(&_id, 0, &run, this)) != 0)
		{
			tools::LOG_ERR_MSG(ret, "Thread::start pthread_create error");
		}
		return ret;
	}

}
